<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="/static/css/mini-nord.min.css">
  <title>HomePage</title>
  <style>
    .bootstrap_top{ 
      height: 5rem;
      margin-top: 2rem;
    }

    .bootstrap_top_left{
      width: 100%;
      height: 100%;
      background-color: #444A5A;
      border-radius: 0.6rem;
    }

    .bootstrap_top_right{
      width: 100%;
      height: 100%;
      background-color: #444A5A;
      border-radius: 0.6rem;
    }

    .bootstrap_bottom{
      height: 50px;
      margin-top: 1.2rem;
    }

    .bootstrap_bottom_left{
      width: 100%;
      height: 100%;
      background-color: #444A5A;
      border-radius: 0.6rem;
    }

    .bootstrap_bottom_right{
      width: 100%;
      height: 100%;
      background-color: #FFFFFF;
      border-radius: 0.6rem;
    }

    .bootstrap_top_left > p{
      margin: 0;
      color: red;
      font-family: 'Courier New', Courier, monospace;
      font-size: x-large;
      line-height: 5rem;
      text-align: center;
    }

    .bootstrap_top_right > a {
      margin-left: 0.8rem;
      line-height: 5rem;
    }

    .bootstrap_top_right > a > img {
      width: 1.5rem;
      height: 1.5rem;
      filter: invert(100%);
    }

    .bootstrap_top_right > div{
      width: 18rem;
      color: #fff;
      line-height: 5rem;
      text-align: center;
      float: right;
    }

  </style>
</head>
<body>

  <div class="container">
    <div class="row">

      <div class="col-md-1 col-lg-2"></div>
      
      <div class="col-sm-12 col-md-10 col-lg-8">
        
        <div class="row bootstrap_top">
          <div class="col-sm-2 col-md-2 col-lg-2">
            <div class="bootstrap_top_left">
              <p>简&礼</p>
            </div>
          </div>
          <div class="col-sm-10 colh-md-10 col-lg-10">
            <div class="bootstrap_top_right">
              <a href="/"><img src="/static/images/home.jpg" alt=""></a>
              <div id="userinfo" class="userinfo"></div>
            </div>
          </div>
        </div>
        
        <div class="row bootstrap_bottom">
          <div class="col-sm-2 col-md-2 col-lg-2">
            <div class="bootstrap_bottom_left">

            </div>
          </div>
          <div class="col-sm-10 colh-md-10 col-lg-10">
            <div class="bootstrap_bottom_right">

            </div>
          </div>
        </div>

      </div>
      
      <div class="col-md-1 col-lg-2"></div>

    </div>
  </div>

  <script>

    function getToken () {
      return localStorage.getItem('token') || ''
    }

    function getMyInfo () {
      let uinfo = localStorage.getItem('userinfo')
      if (uinfo) {
        try {
          return JSON.parse(uinfo)
        } catch (err) {
          return null
        }
      }
      return null
    }

    function checkLogin() {
      let tk = getToken()
      if (!tk) {
        location.href = '/login'
        return
      }

      let uinfo = getMyInfo()

      if (uinfo === null) {
        location.href = '/login'
      } else {
        //登录超时
        if (uinfo.time + uinfo.expires < Date.now()) {
          localStorage.removeItem('token')
          localStorage.removeItem('userinfo')
          location.href = '/login'
        }
      }
    }

    //检测是否登录并跳转
    checkLogin()

    function apicall (path, options = {}) {
      let token = getToken()
      let q = '?'
      if (path.indexOf('?') > 0) {
        q = '&'
      }
      path = `${path}${q}token=${token}`

      return fetch(path, options).then(async res => {
        
        if (!res.ok) {
          let errtext = await res.text()
          throw new Error(`${res.status} : ${errtext}`)
        } else {
          return res.text()
        }

      })

    }

    function getUserInfo (id) {

      return apicall('/user/info/'+id).then(d => {
        let uinfo = JSON.parse(d)
        showUserInfo(uinfo)
      })
      .catch(err => {
        alert(err.message)
      })

    }

    function showUserInfo (u) {
      let html = `
        ${u.nickname} [${u.id}]
        &nbsp;&nbsp;
        <a href="javascript:logout(this);">
          <img src="/static/images/exit.jpg" alt="" style="width: 1.5rem;height: 1.5rem;filter: invert(100%);">
        </a>
      `
      document.getElementById('userinfo').innerHTML = html
    }

    function logout () {
      if (!confirm('确定退出？')) {
        return
      }
      localStorage.removeItem('token')
      localStorage.removeItem('userinfo')
      location.href = '/login'
    }

    window.onload = () => {
      getUserInfo('self')
    }

  </script>
</body>
</html>
